﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class AppCode_Admin_AddPolicyOnEmp : System.Web.UI.Page
{
    int empNo = 0;
    int policyId = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["userNameAdmin"] == null)
            {
                Response.Redirect("..\\Home\\HomePage.aspx");
            }

        }
        int requestId = Convert.ToInt32(Request.QueryString["requestId"]);
        String connect = WebConfigurationManager.ConnectionStrings["ConnectDatabase"].ToString();
        SqlConnection sqlConnect = new SqlConnection(connect);
        sqlConnect.Open();
        SqlCommand sqlCommand = new SqlCommand("Select *from PolicyRequestDetails where requestId=" + requestId, sqlConnect);
        SqlDataReader reader = sqlCommand.ExecuteReader();
        reader.Read();

        empNo = reader.GetInt32(2);
        policyId = reader.GetInt32(3);
        sqlConnect.Close();
        txtUser.Text = GetNameOfEmployee(empNo);
        txtCompanyId.Text = GetcompanyId(policyId).ToString();
        txtPolicyName.Text = GetNameOfPolicy(policyId);

        Label2.Visible = false;
        Label4.Visible = false;
        Label6.Visible = false;
        Label8.Visible = false;
        Label10.Visible = false;
        Label12.Visible = false;
        success.Visible = false;

    }
    public string GetNameOfEmployee(int empNo)
    {
        String connect = WebConfigurationManager.ConnectionStrings["ConnectDatabase"].ToString();
        SqlConnection sqlConnect = new SqlConnection(connect);
        sqlConnect.Open();
        SqlCommand sqlCommand = new SqlCommand("Select username from empRegister where empNo=" + empNo, sqlConnect);
        SqlDataReader reader = sqlCommand.ExecuteReader();
        reader.Read();
        String name = reader.GetString(0);
        sqlConnect.Close();
        return name;
    }

    public String GetNameOfPolicy(int policyId)
    {
        String connect = WebConfigurationManager.ConnectionStrings["ConnectDatabase"].ToString();
        SqlConnection sqlConnect = new SqlConnection(connect);
        sqlConnect.Open();
        SqlCommand sqlCommand = new SqlCommand("Select policyName from policies where policyId=" + policyId, sqlConnect);
        SqlDataReader reader = sqlCommand.ExecuteReader();
        reader.Read();
        String name = reader.GetString(0);
        sqlConnect.Close();
        return name;
    }

    public int GetcompanyId(int policyId)
    {
        String connect = WebConfigurationManager.ConnectionStrings["ConnectDatabase"].ToString();
        SqlConnection sqlConnect = new SqlConnection(connect);
        sqlConnect.Open();
        SqlCommand sqlCommand = new SqlCommand("Select companyId from Policies where policyId=" + policyId, sqlConnect);
        SqlDataReader reader = sqlCommand.ExecuteReader();
        reader.Read();
        int companyid = reader.GetInt32(0);
        return companyid;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Boolean check = false;
        if (txtEndDay.Text.Equals(""))
        {
            Label6.Visible = true;
            check = true;
        }
        if (txtStartDay.Text.Equals(""))
        {
            Label8.Visible = true;
            check = true;
        }
        if (txtDuration.Text.Equals(""))
        {
            Label10.Visible = true;
            check = true;
        }
        if (!check)
        {
            String connect = WebConfigurationManager.ConnectionStrings["ConnectDatabase"].ToString();
            SqlConnection sqlConnect = new SqlConnection(connect);
            sqlConnect.Open();
            SqlCommand sqlCommand = new SqlCommand("insert into PoliciesOnEmployees(Empno,PolicyId,PolicyDuration,PStartDate,PEndDate) values(@Empno,@PolicyId,@PolicyDuration,@PStartDate,@PEndDate)", sqlConnect);
            sqlCommand.Parameters.AddWithValue("@Empno", empNo);
            sqlCommand.Parameters.AddWithValue("@PolicyId", policyId);
            sqlCommand.Parameters.AddWithValue("@PolicyDuration", txtDuration.Text);
            sqlCommand.Parameters.AddWithValue("@PStartDate", txtStartDay.Text);
            sqlCommand.Parameters.AddWithValue("@PEndDate", txtEndDay.Text);
            sqlCommand.ExecuteNonQuery();
            sqlConnect.Close();
            success.Visible = true;
            Button1.Enabled = false;
        }
    }
}